package com.dingding.dubbo.framework;


import java.lang.reflect.Proxy;

/**
 * @program: spring-learning
 * @description:
 * @author: liugongding
 * @create: 2022-02-15 14:55
 **/
public class ProxyFactory {

    public static <T> T  getProxy(Class interfaceClass) {

        Object proxyInstance = Proxy.newProxyInstance(ProxyFactory.class.getClassLoader(), new Class[]{interfaceClass}, (proxy, method, args) -> {
            Invocation invocation = new Invocation(
                    interfaceClass.getName(),
                    method.getName(),
                    method.getParameterTypes(),
                    args);

//            List<URL> urls = RemoteRegister.get(interfaceClass.getName());
//            URL url = LoadBalance.random(urls);
            URL url = new URL("localhost",8080);
            Protocol protocol = ProtocolFactory.getProtocol();
            String result = null;
            try {
                result = protocol.send(url, invocation);
            } catch (Exception e) {
                return "容错信息";
            }
            return result;
        });
        return (T) proxyInstance;
    }
}
